package com.leo.encryptiondemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.TextView;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
 * 非对称加密
 * 加密和解密两套密码
 *
 */
public class RSAActivity extends AppCompatActivity {


    private PrivateKey mAPrivate;
    private String mString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rsa);
        initView();
    }
    private TextView show;
    private void initView() {

        show= (TextView) findViewById(R.id.showKey);
    }
    private PublicKey mAPublic;
    //生成公钥和私钥
    public void generate(View view) {

        new Thread(new Runnable() {



            @Override
            public void run() {
                //获取公钥和私钥的对象
                try {
                    KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");

                    //初始化,密码的长度bit
                    keyPairGenerator.initialize(1024);

                    //获取公钥和私钥
                    KeyPair keyPair = keyPairGenerator.generateKeyPair();

                    //获取私钥
                    //私钥保存好了，一旦丢了，基本找不回
                    mAPrivate = keyPair.getPrivate();
                    //获取公钥
                    //公钥丢了，可以通过私钥推得
                    mAPublic = keyPair.getPublic();
                    //私钥的具体数据，保存该数据，文件中，sp
                    //必须进行base64转换!!!
                    final byte[] encodedPrivate = mAPrivate.getEncoded();


                    final byte[] encodedPublic = mAPublic.getEncoded();
                    //对公钥和私钥进行BASE64编码
                    final byte[] encodePrivate = Base64.encode(encodedPrivate, Base64.DEFAULT);

                    final byte[] encodePublic = Base64.encode(encodedPublic, Base64.DEFAULT);

                    //显示到文本中
                    show.post(new Runnable() {
                        @Override
                        public void run() {
                            show.setText("私钥是:"+"\n"+new String(encodePrivate)+"\n"+"公钥是:"+"\n"+new String(encodePublic));



                        }
                    });


                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }
    //使用公钥私钥进行加密
    public void encrypt(View view) {
    //一般用私钥加密，  使用公钥进行解密
        String check="100000000,123456";
        switch (view.getId()){
            case R.id.rsaEncrypt:
                try {
                    Cipher cipher=Cipher.getInstance("RSA");

                    if (mAPrivate != null) {

                        cipher.init(Cipher.ENCRYPT_MODE,mAPrivate);
                    }
                    byte[] bytes = cipher.doFinal(check.getBytes());
                    byte[] encode = Base64.encode(bytes, Base64.DEFAULT);
                    mString = new String(encode);
                    show.setText("RSA加密数据"+ mString);

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }
                break;

            case R.id.rsaDecrypt:

                try {
                    Cipher cipher=Cipher.getInstance("RSA");
                    if (mAPublic != null) {
                        cipher.init(Cipher.DECRYPT_MODE,mAPublic);
                    }
                    byte[] decode = Base64.decode(mString.getBytes(), Base64.DEFAULT);
                    byte[] bytes = cipher.doFinal(decode);
                    show.setText("RSA解密数据"+new String(bytes));
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }


                break;
        }
    }
}
